// coding: utf-8
// @Author : lryself
// @Date : 2021/3/24 11:43
// @Software: GoLand

package main

import (
	"fmt"
	log "github.com/sirupsen/logrus"
	"golang.org/x/text/encoding/simplifiedchinese"
	"os/exec"
)
var passChan = make(chan string)
var resultChan = make(chan string)

func main() {
	minLen := 10
	maxLen := 10
	rarPath := "F:\\PyCharm知识手册V1.0.rar"
	outPath := "F:\\"
	go func() {
		for i:=minLen;i<=maxLen;i++ {
			getPassword(i, "")
		}
	}()
	for true {
		select {
		case passStr := <- passChan:
			go decompression(passStr, rarPath, outPath)
			//fmt.Println(passStr)
		case result := <- resultChan:
			log.Info("结果是"+result)
			return
		}
	}
}

func decompression(pass string, rarPath string, outPath string){
	cmd := exec.Command("unrar", "e","-p"+pass,rarPath, outPath)  //解压出来保存 D/test 上
	out, err := cmd.Output()
	if err != nil{
		log.Errorln("不是"+pass, err)
		return
	}
	outStr,_ := simplifiedchinese.GBK.NewDecoder().String(string(out))
	fmt.Println(outStr)
	resultChan<-pass
}

func getPassword(passLen int, passStr string)  {
	if len(passStr) >= passLen{
		passChan<-passStr
		return
	}
	//strCaps := "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	//strcaps := "abcdefghijklmnopqrstuvwxyz"
	strcaps := "bopy"
	strNums := "0123456789"
	//strSymbol := "+=-@#~,.[]()!%^*$"

	StrPassRange := strcaps+strNums

	for _,ch := range StrPassRange{
		getPassword(passLen, passStr+string(ch))
	}
}